package Tree.Recursive;

import Utils.TreeNode;

/**
 * 二叉树的直径
 * Lettcode 543: https://leetcode-cn.com/problems/diameter-of-binary-tree/
 */
public class diameterOfBinaryTree {
    int max = 0;

    public int diameterOfBinaryTree(TreeNode root) {
        if (root == null) {
            return 0;
        }
        maxDepth(root);
        return max;
    }

    private int maxDepth(TreeNode root) {
        if (root == null) {
            return 0;
        }
        int left = maxDepth(root.left);
        int right = maxDepth(root.right);
        max = Math.max(max, left + right);
        return Math.max(left, right) + 1;
    }
}
